var express = require('express');
var router = express.Router();
var MongoClient=require('mongodb').MongoClient;
var DB_CONN_STR="mongodb://localhost:27017/product";
var async = require('async');
var multiparty = require('multiparty');
var fs = require('fs');

//首页
router.get('/', function(req, res, next) {
  async.parallel([
    function(cb){
       MongoClient.connect(DB_CONN_STR,function(err,db){
       if(err){
          console.log(err);
          return;
        }else{
          var conn=db.collection('in_theaters');
          conn.find().limit(5).toArray(function(err,results){
            db.close();
            cb(null,results);
          });
        }
      })
    },
    function(cb){
       MongoClient.connect(DB_CONN_STR,function(err,db){
       if(err){
          console.log(err);
          return;
        }else{
          var conn=db.collection('in_theaters');
          conn.find().limit(12).toArray(function(err,results){
            db.close();
            cb(null,results);
          });
        }
      })
    },
    function(cb){
       MongoClient.connect(DB_CONN_STR,function(err,db){
       if(err){
          console.log(err);
          return;
        }else{
          var conn=db.collection('coming_soon');
          conn.find().limit(12).toArray(function(err,results){
            db.close();
            cb(null,results);
          });
        }
      })
    },
    function(cb){
       MongoClient.connect(DB_CONN_STR,function(err,db){
       if(err){
          console.log(err);
          return;
        }else{
          var conn=db.collection('top250');
          conn.find().limit(12).toArray(function(err,results){
            db.close();
            cb(null,results);
          });
        }
      })
    }
  ],function(err,results){
    res.render('index',{data:{
      lunbo:results[0],
      in_theaters:results[1],
      coming_soon:results[2],
      top250:results[3],
      phone:req.session.phone,
    }})
  })
 
});

//登录
router.get('/login',function(req,res,next){
  res.render('login',{});
})

//注册
router.get('/register',function(req,res,next){
  res.render('register',{});
})

//退出
router.get('/logout',function(req,res,next){
  req.session.destroy(function(err){
    res.redirect('/');
  })
})

router.post('/uploadImg',function(req,res,next){
    var form = new multiparty.Form();
    form.encoding = 'utf-8';
    form.uploadDir= './uploadtmp';//临时目录
    form.maxFilesSize = 2*1024*1024;

    form.parse(req,function(err,fields,files){

        var uploadurl = '/images/upload';
        var file = files['filedata'];

        var originalFilename = file[0].originalFilename;
        var tmpPath = file[0].path;

        var timestamp = new Date().getTime();
        uploadurl += timestamp + originalFilename;
        var newPath = './public' + uploadurl;

        var fileReadStream = fs.createReadStream(tmpPath);
        var fileWriteStream = fs.createWriteStream(newPath);
        fileReadStream.pipe(fileWriteStream);
        fileWriteStream.on('close',function(){
          fs.unlinkSync(tmpPath);
          res.send('{"err":"","msg":"'+ uploadurl +'"}')
        })
    })
})

module.exports = router;
